/**
 *
 * @description:
 * @author: junyong.hong
 * @createTime: 2018/6/19
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
const { mysql } = require('../qcloud')

module.exports = async (ctx) => {
    const { bookid, openid } = ctx.request.query
    const mysqlSelect = mysql('comments')
    .select('comments.*', 'csessioninfo.user_info')
    .join('csessioninfo', 'comments.openid', 'csessioninfo.open_id')

    let comments
    // 图书详情页的评论列表
    if (bookid) {
        comments = await mysqlSelect.where('bookid', bookid)
    } else if (openid) {
      // 根据个人的openid帅选
        comments = await mysqlSelect.where('openid', openid)
    }

    ctx.state.data = {
        list: comments.map(v => {
            const info = JSON.parse(v.user_info)
            return Object.assign({}, v, {
                title: info.nickName,
                image: info.avatarUrl
            })
        })
    }
}

